<template>
  <div>
    <el-dialog
        style="  border-radius: 10px"
        @close="$emit('close')"
        v-model="localDialogFormVisible"
        :close-on-click-modal="false"
        title="新建项目"
    >
      <el-form :model="ruleFormRef" :rule="rules" ref="ruleFormRef" label-width="auto">
        <el-form-item label="项目路径" prop="projectPath">
          <el-input v-model="ruleFormRef.projectPath" autocomplete="off">
            <template #append>
              <el-button type="info" :icon="FolderOpened" circle @click="getProjectPath()"/>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="ruleFormRef.projectName" autocomplete="off"/>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="$emit('close')">取消</el-button>
          <el-button type="primary" @click="submitForm">
            确认
          </el-button>
        </div>
      </template>


    </el-dialog>
  </div>
</template>
<script>
import {IniUtils} from "@/utils/iniUtils";
import {ipcRenderer} from "electron";
import {FolderOpened} from "@element-plus/icons-vue";
import {ElMessage} from "element-plus";
import {LogUtils} from "@/utils/logUtils";

export default {
  name: 'NewProject',
  computed: {
    FolderOpened() {
      return FolderOpened
    },
  },
  props: {
    dialogFormVisible: {
      default: false,
      type: Boolean
    },
    projectPath: {
      default: IniUtils.get("window.default.project.path"),
      type: String
    }
  },
  data() {
    return {
      localDialogFormVisible: this.dialogFormVisible,
      ruleFormRef: {
        projectPath: this.projectPath,
        projectName: '',
      },
      rules: {
        projectName: [
          {required: true, message: '请输入项目名称', trigger: 'blur'},
          {min: 1, max: 15, message: '长度应在1到15之前', trigger: 'blur'}
        ],
        projectPath: [
          {required: true, message: '请选择项目路径', trigger: 'blur'}
        ]
      }
    }
  },
  watch: {
    // 监听 dialogFormVisible 的变化
    dialogFormVisible(newValue) {
      LogUtils.info("newProject", newValue? "打开新建项目窗口" : "关闭新建项目窗口")
      this.localDialogFormVisible = newValue;
      ipcRenderer.send("new-project", newValue)
    }
  },
  methods: {
    getProjectPath() {
      ipcRenderer.send("onChoicePath")
    },
    submitForm() {
      if (!this.ruleFormRef.projectPath || !this.ruleFormRef.projectName) {
        ElMessage.error('Oops, this is a error message.')
        return
      }
      // todo 创建初始项目文件夹

    }
  },
  mounted() {
    ipcRenderer.on("choicePathCallback", (event, args) => {
      this.ruleFormRef.projectPath = args
    })
  },
  unmounted() {
    ipcRenderer.removeAllListeners("choicePathCallback")
  }

}

</script>
<style>

</style>